package com.lucy.disk.onlyoffice.service.impl;

import cn.hutool.core.lang.Validator;
import com.lucy.common.constant.HttpStatus;
import com.lucy.common.exception.BusinessException;
import com.lucy.disk.constant.FileConst;
import com.lucy.disk.domain.entity.FileEntity;
import com.lucy.disk.onlyoffice.constant.DocumentConstants;
import com.lucy.disk.onlyoffice.entity.Document;
import com.lucy.disk.onlyoffice.entity.DocumentEditParam;
import com.lucy.disk.onlyoffice.service.DocumentService;
import com.lucy.disk.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Optional;

/**
 * @author kindear
 *
 */
@Slf4j
@Service
public class DocumentServiceImpl implements DocumentService {



    @Value("${server.host}")
    String serverHost;

    @Autowired
    FileService fileService;

    @Override
    public String buildDocument(String filePath, String fileName) {
        return null;
    }

    @Override
    public Document getDocument(String documentKey) {
        // 如果不为空 获取文件信息
        Optional<FileEntity> fileOp = fileService.getOne(documentKey);
        if (!fileOp.isPresent()){
            throw new BusinessException(HttpStatus.NOT_FOUND,"文件key对应文件不存在");
        }
        FileEntity file = fileOp.get();
        Document document = new Document();
        document.setKey(file.getId());
        document.setTitle(file.getFileName());
        document.setFileType(file.getExtName());
        document.setStorage(file.getOssUrl());
        String url = serverHost+"/disk/download/"+file.getId();
        document.setUrl(url);
        return document;
    }

    @Override
    public void downloadDocumentFile(String documentKey, HttpServletRequest request, HttpServletResponse response) throws IOException {

    }

    @Override
    public DocumentEditParam buildDocumentEditParam(String userId, String userName) {
        return DocumentEditParam.builder()
                .callbackUrl(callbackUrl())
                .user(DocumentEditParam.UserBean.builder()
                        .id(userId)
                        .name(userName)
                        .build())
                .build();
    }

    @Override
    public boolean saveDocumentFile(String documentKey, String downloadUrl) throws IOException {
        Optional<FileEntity> fileOp = fileService.getOne(documentKey);
        if (!fileOp.isPresent()){
            throw new BusinessException(HttpStatus.NOT_FOUND);
        }
        FileEntity file = fileOp.get();

        return false;
    }

    private String callbackUrl() {
        return serverHost + "/disk/editor/callback";
    }


    @Override
    public Object getServerHost() {
        return serverHost;
    }

    @Override
    public boolean canEdit(Document document) {
        if (FileConst.DOC_FILE.contains(document.getFileType()) && !document.getFileType().equals("pdf")){
            return true;
        }
        return false;
    }
}
